/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.xtd.facede.retail;

import com.xtd.facade.AbstractFacade;
import com.xtd.model.retail.Lead;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;

import javax.ejb.Stateless;

/**
 *
 * @author Lu Yuanbin
 */
@Stateless
public class LeadFacade extends AbstractFacade<Lead> {

    @EJB
    AccountFacade accountFacade;

    public LeadFacade() {
        super(Lead.class);
    }

    public List<Lead> find(String searchContent) {
        if (searchContent == null) {
            searchContent = "";
        }
        return getEntityManager().createQuery("select l from Lead as l where l.account.mname like :searchContent or l.account.fname like :searchContent or l.account.mphone like :searchContent or l.account.fphone like :searchContent or l.account.phone like :searchContent ORDER BY l.createdTime DESC").setParameter("searchContent", "%" + searchContent + "%").getResultList();
    }

    public Lead update(Lead lead) {
        if (lead.getAccount() == null) {
            throw new IllegalArgumentException("未初始化客流量的客户信息");
        } else {
            accountFacade.update(lead.getAccount());
        }
        if (lead.getId() == null) {
            getEntityManager().persist(lead);
        } else {
            lead = getEntityManager().merge(lead);
        }
        return lead;
    }

    public List<Lead> find(Date startDate, Date endDate) {
        return getEntityManager().createQuery("select l from Lead as l where l.visitTime between :startDate and :endDate order by l.visitTime desc").setParameter("startDate", startDate).setParameter("endDate", endDate).getResultList();
    }
}
